package com.lykj.modules.shop.controller;

import com.alibaba.fastjson.JSON;
import com.lykj.core.excel.util.ExcelUtil;
import com.lykj.core.secure.utils.AuthUtil;
import com.lykj.core.tool.utils.DateUtil;
import com.lykj.modules.shop.excel.UserBonusExcel;
import com.lykj.modules.shop.excel.UserExtractExcel;
import com.lykj.modules.shop.vo.UserBillVO;
import com.lykj.modules.shop.vo.UserExtractVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.lykj.core.mp.support.Condition;
import com.lykj.core.mp.support.Query;
import com.lykj.core.tool.api.R;
import com.lykj.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.modules.shop.entity.UserBonus;
import com.lykj.modules.shop.vo.UserBonusVO;
import com.lykj.modules.shop.wrapper.UserBonusWrapper;
import com.lykj.modules.shop.service.UserBonusService;
import com.lykj.core.boot.ctrl.LykjController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 红包表 控制器
 *
 * @author cxl
 * @since 2022-01-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("lykj-shop/user/bonus")
@Api(value = "红包表", tags = "红包表接口")
public class UserBonusController extends LykjController {

	private final UserBonusService userBonusService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入userBonus")
	public R<UserBonusVO> detail(UserBonus userBonus) {
		UserBonus detail = userBonusService.getOne(Condition.getQueryWrapper(userBonus));
		return R.data(UserBonusWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 红包表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userBonus")
	public R<IPage<UserBonusVO>> list(@ApiIgnore @RequestParam Map<String, Object> map, Query query) {//list(UserBonusVO userBonus, Query query) {
//		if(query.getAscs()==null && query.getDescs()==null) {
			query.setDescs("id");
//		}
		UserBonusVO userBonus = JSON.parseObject(JSON.toJSONString(map), UserBonusVO.class);
//		IPage<UserBonus> pages = userBonusService.page(Condition.getPage(query), Condition.getQueryWrapper(userBonus));
//		return R.data(UserBonusWrapper.build().pageVO(pages));
		IPage<UserBonusVO> pages = userBonusService.selectUserBonusPage(Condition.getPage(query), userBonus);
		return R.data(pages);
	}


	/**
	 * 自定义分页 红包表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入userBonus")
	public R<IPage<UserBonusVO>> page(UserBonusVO userBonus, Query query) {
		userBonus.setUserId(AuthUtil.getUserId());
//		if(query.getAscs()==null && query.getDescs()==null) {
			query.setDescs("id");
//		}
		IPage<UserBonusVO> pages = userBonusService.selectUserBonusPage(Condition.getPage(query), userBonus);
		return R.data(pages);
	}

	/**
	 * 新增 红包表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入userBonus")
	public R save(@Valid @RequestBody UserBonus userBonus) {
		return R.status(userBonusService.save(userBonus));
	}

	/**
	 * 修改 红包表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入userBonus")
	public R update(@Valid @RequestBody UserBonus userBonus) {
		return R.status(userBonusService.updateById(userBonus));
	}

	/**
	 * 新增或修改 红包表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入userBonus")
	public R submit(@Valid @RequestBody UserBonus userBonus) {
		return R.status(userBonusService.saveOrUpdate(userBonus));
	}


	/**
	 * 删除 红包表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userBonusService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出红包
	 */
	@GetMapping("export")
	@ApiOperationSupport(order = 15)
	@ApiOperation(value = "导出红包")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		UserBonusVO userBonus = JSON.parseObject(JSON.toJSONString(params), UserBonusVO.class);
		if(Func.isNotEmpty(params.get("queryDate")) && !"undefined".equals(params.get("queryDate"))) {
			String[] date = params.get("queryDate").toString().split(",");
			userBonus.setStartDate(date[0]);
			userBonus.setEndDate(date[1]);
		}
		List<UserBonusExcel> listExcel = userBonusService.export(userBonus);
		ExcelUtil.export(response, "用户红包数据" + DateUtil.time(), "用户红包数据", listExcel, UserBonusExcel.class);
	}
}
